package com.youfang.sixthmedia.ui.detail;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.gzit.common.async.AsynCallback;
import com.gzit.common.async.LoadingCallback;
import com.youfang.sixthmedia.R;
import com.youfang.sixthmedia.common.BuddyAdapter;
import com.youfang.sixthmedia.common.YoufangContext;
import com.youfang.sixthmedia.controller.HeaderController;
import com.youfang.sixthmedia.message.GetBuddyNearByBuildingRequest;
import com.youfang.sixthmedia.message.GetBuddyNearByBuildingResponse;
import com.youfang.sixthmedia.message.model.Buddy;
import com.youfang.sixthmedia.message.model.DetailBuilding;
import com.youfang.sixthmedia.net.Net;
import com.youfang.sixthmedia.net.Response;
import com.youfang.sixthmedia.widget.GZITPullToRefreshListView;
import com.youfang.sixthmedia.widget.GZITPullToRefreshListView.OnBottomListener;
import com.youfang.sixthmedia.widget.GZITPullToRefreshListView.OnRefreshListener;

public class DetailBuildingBuddyActivity extends Activity {

	private int page = 1;
	private HeaderController controller;
	private GZITPullToRefreshListView listview;
	private BuddyAdapter adapter;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.detail_building_buddy_activity);

		DetailBuilding detail = YoufangContext.getCurrentBuilding();
		if (detail == null) {
			return;
		}

		this.listview = (GZITPullToRefreshListView) this
				.findViewById(R.id.listview);

		this.adapter = new BuddyAdapter(this);
		this.listview.setAdapter(adapter);
		this.controller = new HeaderController();
		this.controller.setup(listview, adapter);

		this.listview.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {

				remoteRefresh();

			}
		});
		this.listview.setOnBottomListener(new OnBottomListener() {

			@Override
			public void onBottomClick() {
				remoteMore();
			}

			@Override
			public void onBottom() {

			}
		});

		//
		// this.dataItem = new DetailBuilding();
		// dataItem.buildingname = "中州中央公园";
		// dataItem.id = "5016";

		((TextView) this.findViewById(R.id.title)).setText(detail.buildingname);

		remoteRefresh();

	}

	public void remoteRefresh() {
		DetailBuilding detail = YoufangContext.getCurrentBuilding();
		if (detail == null) {
			return;
		}

		// 重置页码
		page = 1;
		GetBuddyNearByBuildingRequest req = new GetBuddyNearByBuildingRequest();
		req.setLat("" + detail.getLatitudeE6());
		req.setLon("" + detail.getLongitudeE6());

		req.setPage(page);

		Net.doHttpRemote(this, req, new GetBuddyNearByBuildingResponse(),
				new AsynCallback<Response>() {

					@Override
					public void onFinish(Response r) {
						super.onFinish(r);
						listview.onRefreshComplete();
					}

					@Override
					public void callback(Response r) {
						if (!r.isOk()) {
							return;
						}

						GetBuddyNearByBuildingResponse resp = (GetBuddyNearByBuildingResponse) r;
						List<Buddy> dataList = resp.getDataList();
						adapter.reload(dataList);
						adapter.notifyDataSetChanged();
						listview.setSelection(1);

						// 控制楼房图标
						controller.onRefresh(dataList);

						// 页码递增
						page++;

					}
				}, LoadingCallback.EMPTY);

	}

	public void remoteMore() {

		DetailBuilding detail = YoufangContext.getCurrentBuilding();
		if (detail == null) {
			return;
		}

		GetBuddyNearByBuildingRequest req = new GetBuddyNearByBuildingRequest();
		req.setLat("" + detail.getLatitudeE6());
		req.setLon("" + detail.getLongitudeE6());
		req.setPage(page);

		Net.doHttpRemote(this, req, new GetBuddyNearByBuildingResponse(),
				new AsynCallback<Response>() {

					@Override
					public void onFinish(Response r) {
						super.onFinish(r);
						listview.onBottomFinish();
					}

					@Override
					public void callback(Response r) {
						if (!r.isOk()) {
							return;
						}

						GetBuddyNearByBuildingResponse resp = (GetBuddyNearByBuildingResponse) r;
						List<Buddy> dataList = resp.getDataList();

						adapter.addAll(dataList);
						adapter.notifyDataSetChanged();

						// logo控制器
						controller.onMore(dataList);

						// 页码递增
						page++;

					}
				}, LoadingCallback.EMPTY);

	}

	@Override
	protected void onStop() {
		super.onStop();
		controller.onStop();
	}

}
